import cv2
import json
import os
import pdb

json_dir = '/data/lzy/tile_round1_train_20201231/coco/annotations/instances_train2017.json'
image_dir = '/data/lzy/coco_bak/all/'
json2_dir = '/data/lzy/coco_bak/all_train.json'
save_dir = '/data/lzy/coco_bak/vis'
with open(json2_dir) as f:
    annos2=json.load(f)
with open(json_dir) as f:
    annos=json.load(f)
# pdb.set_trace()
c = 0
for i in range(len(annos2)):
    anno=annos2[i]
    name = anno['name']
    img_path = os.path.join(image_dir, name)
    coors = anno['bbox']
    x1 = (int(coors[0]), int(coors[1]))
    x2 = (int(coors[2]), int(coors[3]))
    image = cv2.imread(img_path)
    image = cv2.rectangle(image, x1, x2, (255, 0, 0), 1)
    image = cv2.putText(image, str(anno['category']), x1, cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
    path = os.path.join(save_dir, name.split('.')[0]+'_{}.jpg'.format(c))
    c += 1
    cv2.imwrite(path, image)

